$(function () {
    findAll();
    // setEnableHtml();
});

function setEnableHtml() {
    let data = myAjax("/back/user/enable", {}, "get");
    let html = '<option value=0>全部</option>';
    for (let i = 0; i < data.length; i++) {
        html += '<option value="'+data[i].enable+'">'+data[i].value+'</option>';
    }
    $("#enable").html(html)
}

let curr = 1;
let limit = 10;
let count = 0;

function findAll() {
    // $.ajax({
    //     url: '/back/user/findAll',
    //     data: '',
    //     type: 'get',
    //     dataType: 'json',
    //     success: function (data) {
    //         console.log(data);
    //         setMemberListData(data.list);
    //     }
    // })
    var arr_box = [];
    $('input[type=checkbox][name=hobby]:checked').each(function () {
        arr_box.push($(this).val());
    });

    let da = {
        username: $("#username").val(),
        phone: $("#phone").val(),
        sex: $("#sex").val(),
        email: $("#email").val(),
        start: $("#start").val(),
        end: $("#end").val(),
        enable: $("#enable").val(),
        hobby: arr_box.toLocaleString(),
        page: curr,
        pageSize: limit
    };
    let data = myAjax("/back/user/findAll", da, "get");

    count = data.bean.count;
    setMemberListData(data.bean.list);
}

function setMemberListData(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += '<tr>\n' +

            '                                    <td>' + data[i].id + '</td>\n' +
            '                                    <td>' + data[i].username + '</td>\n' +
            '                                    <td>' + data[i].sex + '</td>\n' +
            '                                    <td>' + nullTOString(data[i].phone) + '</td>\n' +
            '                                    <td>' + nullTOString(data[i].email) + '</td>\n' +
            '                                    <td>' + nullTOString(data[i].birthday) + '</td>\n' +
            '                                    <td>' + nullTOString(data[i].hobby) + '</td>\n' +
            '                                    <td class="td-status">\n' +
            '                                      <span class="layui-btn layui-btn-normal layui-btn-mini ' + (data[i].enable == 1 ? '' : 'layui-btn-disabled') + '">' + (data[i].enable == 1 ? '已启用' : '已停用') + '</span></td>\n' +
            '                                    <td class="td-manage">\n' +
            '                                      <a onclick="member_stop(this,' + data[i].id + ')" href="javascript:;"  title="' + (data[i].enable == 1 ? '启用' : '停用') + '">\n' +
            '                                        <i class="layui-icon">&#xe601;</i>\n' +
            '                                      </a>\n' +
            '                                      <a title="编辑"  onclick="edit(' + data[i].id + ')" href="javascript:;">\n' +
            '                                        <i class="layui-icon">&#xe642;</i>\n' +
            '                                      </a>\n' +
            '                                      <a onclick="xadmin.open(\'修改密码\',\'member-password.html\',600,400)" title="修改密码" href="javascript:;">\n' +
            '                                        <i class="layui-icon">&#xe631;</i>\n' +
            '                                      </a>\n' +
            '                                      <a title="删除" onclick="member_del(this,' + data[i].id + ')" href="javascript:;">\n' +
            '                                        <i class="layui-icon">&#xe640;</i>\n' +
            '                                      </a>\n' +
            '                                    </td>\n' +
            '                                  </tr>';
    }
    $("#tbody").html(html);
}

function nullTOString(value) {
    return value == null ? "" : value;
}

// function openUserAdd() {
//     xadmin.open('添加用户','/html/user/member-add.html',600,400);
//     return false;
// }
function edit(id) {
    sessionStorage.setItem("userId", id);
    xadmin.open('编辑', '/html/user/member-edit.html', 600, 400);
}